package javabase.demo01;

// 封装示例
class BankAccount{
    private String accountNumber;
    private double balance;

    // 构造方法
    public BankAccount(String accountNumber, double initialBalance){
        this.accountNumber = accountNumber;
        this.balance = initialBalance;
    }

    // 封装的方法
    public void deposit(double amount){
        if(amount > 0){
            this.balance = this.balance + amount;
            System.out.println("存款成功，当前余额: " + balance);
        }
    }

    public void withdraw(double amount){
        if(amount >0 && amount <= this.balance){
            this.balance = this.balance - amount;
            System.out.println("取款成功，当前余额: " + balance);
        }else{
            System.out.println("取款失败，余额不足");
        }
    }

    // getter方法
    public double getBalance(){
        return this.balance;
    }

    public String getAccountNumber(){
        return this.accountNumber;
    }
}

class Animal{
    private String name;

    public Animal(String name){
        this.name = name;
    }

    public void makeSound(){
        System.out.println("动物发出声音");
    }

    public String getName(){
        return name;
    }
}

class Dog extends Animal{

    public Dog(String name) {
        super(name); // 调用父类构造方法
    }

    @Override
    public void makeSound() {
        System.out.println(this.getName() + "汪汪叫");
    }

    // super关键字使用示例
    public void superMakeSound() {
        super.makeSound(); // 调用父类方法
    }
}

class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }

    @Override
    public void makeSound() {
        System.out.println(getName() + "喵喵叫");
    }
}
// 抽象类与接口
abstract class Shape{
    protected String color;

    public Shape(String color) {
        this.color = color;
    }

    // 抽象方法
    public abstract double area();

    // 具体方法
    public String getColor() {
        return color;
    }
}

interface Drawable{
    void draw(); // 接口方法默认public
    default void display() { // 默认方法
        System.out.println("展示图形");
    }
    static void printInfo() { // 静态方法
        System.out.println("这是一个可绘制接口");
    }
}

class Circle extends Shape implements Drawable{
    private double radius;

    public Circle(String color, double radius){
        super(color);
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI * this.radius * this.radius;
    }

    @Override
    public void draw() {
        System.out.println("绘制一个" + color + "的圆形，半径: " + radius);
    }
}

// 内部类示例
class OuterClass {
    private int outerField = 10;

    // 成员内部类
    class InnerClass {
        void display() {
            System.out.println("访问外部类字段: " + outerField);
        }
    }

    // 静态内部类
    static class StaticNestedClass {
        void display() {
            System.out.println("静态内部类");
        }
    }

    // 方法内部类
    void methodWithClass() {
        class LocalClass {
            void display() {
                System.out.println("方法内部类");
            }
        }

        LocalClass local = new LocalClass();
        local.display();
    }
}

// 面向对象编程实践x
public class OOPPractice {
    public static void main(String[] args) {
        // 封装测试
        BankAccount account = new BankAccount("123456", 1000);
        account.deposit(500);
        account.withdraw(200);
        System.out.println("最终余额: " + account.getBalance());


        // 继承与多态测试
        Animal myDog = new Dog("Buddy");
        Animal myCat = new Cat("Whiskers");

        myDog.makeSound(); // 多态：运行时调用Dog的方法
        myCat.makeSound(); // 多态：运行时调用Cat的方法

        // 抽象类与接口测试
        Circle circle = new Circle("红色", 5.0);
        System.out.println("圆形面积: " + circle.area());
        circle.draw();
        circle.display();
        Drawable.printInfo(); // 调用接口静态方法

        // 内部类测试
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();
        inner.display();

        OuterClass.StaticNestedClass staticNested = new OuterClass.StaticNestedClass();
        staticNested.display();

        outer.methodWithClass();
    }
}
